<template>
  <div class="with-navigation">
    <NavigationBar :role="userStore.role" />
    <div class="main-content">
      <!-- 顶部标题栏 -->
      <el-header class="system-header">
        <h1 class="system-title">生鲜供应链管理系统</h1>
        <!-- 用户信息和退出 -->
        <div class="user-info">
          <el-dropdown @command="handleCommand" placement="bottom-end">
            <span class="user-dropdown-link">
              {{ userStore.user?.username || '用户' }}
              <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <!-- 主内容区域 -->
      <el-main>
        <router-view />
      </el-main>
    </div>
  </div>
</template>

<script setup>
import NavigationBar from '@/components/NavigationBar.vue'
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import {
  ArrowDown
} from '@element-plus/icons-vue'
import '@/assets/styles/navigation.css'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 激活的菜单项
const activeMenu = computed(() => route.path)

// 处理下拉菜单命令
async function handleCommand(command) {
  if (command === 'profile') {
    // 根据角色跳转到对应的个人中心页面
    const rolePrefix = `/${userStore.role}`
    router.push(`${rolePrefix}/profile`)
  } else if (command === 'logout') {
    await userStore.logout()
    ElMessage.success('已退出登录')
    router.push('/login')
  }
}
</script>

<style scoped>
:root {
  --nav-width: 60px;
}

.with-navigation {
  display: flex;
  min-height: 100vh;
  position: relative;
  width: 100%;
  overflow: hidden;
}

.main-content {
  position: absolute;
  left: var(--nav-width, 60px);
  right: 0;
  top: 0;
  bottom: 0;
  transition: all 0.25s ease-out;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow-y: auto;
  transform: translateX(0);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  will-change: transform, left;
}

.system-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--spacing-md) var(--spacing-lg);
  background-color: var(--background-color);
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

.system-title {
  text-align: left;
  margin: 0;
  color: var(--text-primary);
  font-size: 1.5rem;
  font-weight: 600;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-dropdown-link {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: var(--text-primary);
}

.user-dropdown-link .el-icon {
  margin-left: 5px;
}
</style> 